package zj.com.util;

import android.app.AlarmManager;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by xi on 16/6/21.
 */
public class DateUtil {


    public static String formatStrimgTime(String time){


        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date date = null;
        try {
            date = dateFormat.parse(time);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String str = format.format(date);
            return str;
        }catch (Exception e){

        }
        return null;


    }
    public static String formatStrimgDate(String time){


        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date date = null;
        try {
            date = dateFormat.parse(time);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String str = format.format(date);
            return str;
        }catch (Exception e){

            return time;
        }



    }


    public static String formatStrimgDate2(String time){


        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = dateFormat.parse(time);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String str = format.format(date);
            return str;
        }catch (Exception e){

        }
        return null;


    }

    public static String formatStrimgDate3(String time){


        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        Date date = null;
        try {
            date = dateFormat.parse(time);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String str = format.format(date);
            return str;
        }catch (Exception e){

        }
        return null;


    }

    public static int compare_date(String DATE1, String DATE2) {


        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    public static String getCreateAt(Date date) {
        Calendar c = Calendar.getInstance();
        if (c.get(Calendar.YEAR) - (date.getYear() + 1900) > 0) {
            int i = c.get(Calendar.YEAR) - date.getYear();
            return i + "年前";
        } else if (c.get(Calendar.MONTH) - date.getMonth() > 0) {
            int i = c.get(Calendar.MONTH) - date.getMonth();
            return i + "月前";
        } else if (c.get(Calendar.DAY_OF_MONTH) - date.getDate() > 0) {
            int i = c.get(Calendar.DAY_OF_MONTH) - date.getDate();
            return i + "天前";
        } else if (c.get(Calendar.HOUR_OF_DAY) - date.getHours() > 0) {
            int i = c.get(Calendar.HOUR_OF_DAY) - date.getHours();
            return i + "小时前";
        } else if (c.get(Calendar.MINUTE) - date.getMinutes() > 0) {
            int i = c.get(Calendar.MINUTE) - date.getMinutes();
            return i + "分钟前";
        } else {
            return "刚刚";
        }
    }

    public static String getCreateAt(long addTime) {
        return getCreateAt(new Date(addTime));
    }

//    public static String getCreateAt(long addTime) {
//        Calendar c = Calendar.getInstance();
//        Date date = new Date(addTime);
//        if (c.get(Calendar.DAY_OF_MONTH) - date.getDate() > 0) {
//            int i = c.get(Calendar.DAY_OF_MONTH) - date.getDate();
//            return i + "天前";
//        } else {
//            return getHchhmmWith24(addTime);
//        }
//    }

    public static String getCreateAt(String strDate) {
        if (strDate == null) {
            return "刚刚";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate = simpleDateFormat.parse(strDate);
            return getCreateAt(startDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "刚刚";
    }

    public static String getShorTime(String strDate) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date shortDate = simpleDateFormat.parse(strDate);

            return shortFormat.format(shortDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return "刚刚";
    }

    public static long getPostTimeMiilis(String strDate) {
        if (strDate == null) {
            return System.currentTimeMillis();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        try {
            Date startDate = simpleDateFormat.parse(strDate);
            return startDate.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static long getContentTimeMiilis(String strDate) {
        if (strDate == null) {
            return System.currentTimeMillis();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate = simpleDateFormat.parse(strDate);
            return startDate.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static String getTimeDescription(Date date) {
        Calendar c = Calendar.getInstance();
        //年前年后
        int ret = c.get(Calendar.YEAR) - (date.getYear() + 1900);
        if (ret > 0) {
            int i = c.get(Calendar.YEAR) - date.getYear();
            return i + "年前";
        } else if (ret < 0) {
            int i = date.getYear() - c.get(Calendar.YEAR);
            return i + "年后";
        }

        ret = c.get(Calendar.MONTH) - date.getMonth();
        if (ret > 0) {
            return Math.abs(ret) + "个月前";
        } else if (ret < 0) {
            return Math.abs(ret) + "个月后";
        }

        ret = c.get(Calendar.DAY_OF_MONTH) - date.getDate();
        if (ret > 0) {
            return Math.abs(ret) + "天前";
        } else if (ret < 0) {
            return Math.abs(ret) + "天后";
        }

        ret = c.get(Calendar.HOUR_OF_DAY) - date.getHours();
        if (ret > 0) {
            return Math.abs(ret) + "小时前";
        } else if (ret < 0) {
            return Math.abs(ret) + "小时后";
        }

        ret = c.get(Calendar.MINUTE) - date.getMinutes();
        if (ret > 0) {
            return Math.abs(ret) + "分钟前";
        } else if (ret < 0) {
            return Math.abs(ret) + "分钟后";
        }

        return "刚刚";
    }

    public static String getDeadline(String strDate) {
        if (strDate == null) {
            return "刚刚";
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate = simpleDateFormat.parse(strDate);
            return getTimeDescription(startDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "刚刚";
    }

    public static String getRequestDateNotTime(long millis) {
        if (millis < 0) {
            return null;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(new Date(millis));
    }

    public static String getRequestDate(long millis, String format) {
        if (millis < 0) {
            return null;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date(millis));
    }

    public static String getRequestDate(long millis) {
        if (millis < 0) {
            return null;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return simpleDateFormat.format(new Date(millis));
    }

    public static Calendar longToDateTime(long time) {
        if (time == -1) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.clear();
        c.setTimeInMillis(time);
        return c;
//		return new DateTime(c.get(Calendar.YEAR),
//				c.get(Calendar.MONTH) + 1,
//				c.get(Calendar.DAY_OF_MONTH));
    }

    public static long DateTimeToLong(Calendar date) {
        if (date == null) {
            return -1;
        }
//		Calendar c = Calendar.getInstance();
//		c.clear();
//		c.set(date.getYear(), date.getMonth() - 1, date.getDayOfMonth(),0,0,0);
        return date.getTimeInMillis();
    }

    public static long getCalendar(int year, int month, int day, int hourOfDay, int minute, int second) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(year, month, day, hourOfDay, minute, second);
        return c.getTimeInMillis();
    }

    public static long getCalendar(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(year, month, day, 0, 0, 0);
        return c.getTimeInMillis();
    }

    public static int getAgeBySouth(long time) {
        Calendar c = Calendar.getInstance();
        c.clear();
        c.setTimeInMillis(System.currentTimeMillis());
        int yearNow = c.get(Calendar.YEAR);

        c.clear();
        c.setTimeInMillis(time);
        int yearBirthday = c.get(Calendar.YEAR);
        return yearNow - yearBirthday + 1;
    }

    public static int getAgeByNorth(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(System.currentTimeMillis());
        int yearNow = c.get(Calendar.YEAR);
        int monthNow = c.get(Calendar.MONTH) + 1;
        int dayNow = c.get(Calendar.DAY_OF_MONTH);

        c.setTimeInMillis(time);
        int yearBirthday = c.get(Calendar.YEAR);
        int monthBirthday = c.get(Calendar.MONTH) + 1;
        int dayBirthday = c.get(Calendar.DAY_OF_MONTH);

        boolean flag = true;
        if (monthNow < dayBirthday || (monthNow == monthBirthday && dayNow <= dayBirthday)) {
            flag = false;
        }
        if (flag) {
            return yearNow - yearBirthday;
        } else {
            return yearNow - yearBirthday - 1;
        }
    }

    public static long reformTime(long time) {
        Calendar cal = Calendar.getInstance();
        long seconds = (long) (time / 1000);
        time = seconds * 1000;
        cal.setTimeInMillis(time);
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        return cal.getTimeInMillis();
    }

    public static String getDisplayDate(long time,String timeFormat){
        SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
        return sdf.format(new Date(time));
    }

    public static String getWeekString(long time) {
        String weekStr = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        switch (week) {
            case 0:
                weekStr = "星期日";
                break;
            case 1:
                weekStr = "星期一";
                break;
            case 2:
                weekStr = "星期二";
                break;
            case 3:
                weekStr = "星期三";
                break;
            case 4:
                weekStr = "星期四";
                break;
            case 5:
                weekStr = "星期五";
                break;
            case 6:
                weekStr = "星期六";
                break;
        }
        return weekStr;
    }
    /**
     * 判断是否为今天
     * @param t
     * @return
     */
    public static boolean isToday(long t) {
        final Calendar c = Calendar.getInstance();
        c.setTimeInMillis(t);
        int y = c.get(Calendar.YEAR);
        int dy = c.get(Calendar.DAY_OF_YEAR);
        long time = System.currentTimeMillis();
        c.setTimeInMillis(time);
        int cy = c.get(Calendar.YEAR);
        int cdy = c.get(Calendar.DAY_OF_YEAR);
        if (y == cy && dy == cdy) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否为过去
     * @param t
     * @return
     */
    public static boolean isPast(long t) {
        return t < DateUtil.getDayStartTimer(System.currentTimeMillis());
    }

    /**
     * 判断是否为将来
     * @param t
     * @return
     */
    public static boolean isFuture(long t) {
        return t > DateUtil.getDayEndTimer(System.currentTimeMillis());
    }

    public static String getyyyymmddhhmm(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm",
                Locale.getDefault());
        return sdf.format(time);
    }


    public static String getZHyyyyMMdd(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日",
                Locale.getDefault());
        return sdf.format(time);
    }

    public static String getZHTime(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm",
                Locale.getDefault());
        return sdf.format(time);
    }

    /**
     * 获取月日
     *
     * @param time
     * @return
     */
    public static String getHcMMdd(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日",
                Locale.getDefault());
        return sdf.format(time);
    }

    /**
     * 获取时分
     *
     * @param time
     * @return
     */
    public static String getHchhmm(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm",
                Locale.getDefault());
        return sdf.format(time);
    }

    /**
     * 获取时分
     *
     * @param time
     * @return
     */
    public static String getHchhmmWith24(long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        return sdf.format(time);
    }

    /**
     * 获取某天的开始时间
     *
     * @param time
     * @return
     */
    public static long getDayStartTimer(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime().getTime();
    }

    /**
     * 获取某天的截止时间
     *
     * @param time
     * @return
     */
    public static long getDayEndTimer(long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime().getTime();
    }

    /**
     * 计算两个时间点间隔的天数
     *
     * @param sTime
     * @param eTime
     * @return
     */
    public static int getDaysBetween(long sTime, long eTime) {
        return (int) ((sTime - eTime) / AlarmManager.INTERVAL_DAY);
    }

    public static String getyyyymmdd(long time, String spStr) {
        String p = "yyyy" + spStr + "MM" + spStr + "dd";
        SimpleDateFormat sdf = new SimpleDateFormat(p,
                Locale.getDefault());
        return sdf.format(time);
    }
}
